/*
 * Copyright 2002 Christopher L. Conway, Cheng-Hong Li and Megan Pengelly.
 *
 * This file is part of The Pencil Compiler.
 *
 * The Pencil Compiler is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * The Pencil Compiler is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with The Pencil Compiler; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/* @author Megan Pengelly */
package math.petrinets.model;

import java.awt.*;
import java.io.*;
import java.util.Hashtable;
import java.util.Scanner;

public class PetriTest {
	
	public PetriNet readFromFile(String f) {
		int V	[]		=	null;
		int d	[]		=	null;
		int M0	[]	=	null;
		int Mf	[]	=	null;
		int C	[][]	=	null;
		int Pre[][]	=	null;
		int Post[][]	=	null;
		try {
			Scanner in = new Scanner(new FileReader(f));

			do {
				String l = in.nextLine().trim();
				if (l.startsWith("#") || l.isEmpty()) {
					// ignore comments and empty lines
					continue;
				} else {
					if (l.startsWith("C=")) {
						// C=Incidence Matrix
						// C=#places/#transitions
						// Read Line by line (each line a place)
						// Columns separated by comma (transitions)
						String data[] = l.split("=");
						if (data.length == 2) {
							String dimension[] = data[1].split("/");
							String places = dimension[0];
							String trans = dimension[1];
							C = new int[Integer.parseInt(places)][Integer
									.parseInt(trans)];
							for (int i = 0; i < C.length; i++) {
								String row = null;
								while ((row = in.nextLine()) != null
										&& (row.startsWith("#") || row
												.isEmpty()));
								String values[] = row.split(",");
								for (int j = 0; j < values.length
										&& values.length == C[i].length; j++) {
									C[i][j] = Integer
											.parseInt(values[j].trim());
								}
							}
						}
						continue;
					}
					if (l.startsWith("Pre=")) {
						// Pre=Pre Matrix
						// Pre=#places/#transitions
						// Read Line by line (each line a place)
						// Columns separated by comma (transitions)
						String data[] = l.split("=");
						if (data.length == 2) {
							String dimension[] = data[1].split("/");
							String places = dimension[0];
							String trans = dimension[1];
							Pre = new int[Integer.parseInt(places)][Integer
									.parseInt(trans)];
							for (int i = 0; i < C.length; i++) {
								String row = null;
								while ((row = in.nextLine()) != null
										&& (row.startsWith("#") || row
												.isEmpty()))
									;
								String values[] = row.split(",");
								for (int j = 0; j < values.length
										&& values.length == Pre[i].length; j++) {
									Pre[i][j] = Integer.parseInt(values[j]
											.trim());
								}
							}
						}
						continue;
					}
					if (l.startsWith("V=")) {
						// V=Parick Vector
						// V=#transitions
						String data[] = l.split("=");
						if (data.length == 2) {
							String dimension = data[1];
							V = new int[Integer.parseInt(dimension)];
							String row = null;
							while ((row = in.nextLine()) != null
									&& (row.startsWith("#") || row.isEmpty()))
								;
							String values[] = row.split(",");
							for (int j = 0; j < values.length
									&& values.length == V.length; j++) {
								V[j] = Integer.parseInt(values[j].trim());
							}
						}
						continue;
					}
					if (l.startsWith("d=")) {
						// d=Transitions cost
						// d=#transitions
						String data[] = l.split("=");
						if (data.length == 2) {
							String dimension = data[1];
							d = new int[Integer.parseInt(dimension)];
							String row = null;
							while ((row = in.nextLine()) != null
									&& (row.startsWith("#") || row.isEmpty()));
							String values[] = row.split(",");
							for (int j = 0; j < values.length
									&& values.length == d.length; j++) {
								d[j] = Integer.parseInt(values[j].trim());
							}
						}
						continue;
					}
					if (l.startsWith("M0=")) {
						// M0=Initial Marking
						// M0=#places
						String data[] = l.split("=");
						if (data.length == 2) {
							String dimension = data[1];
							M0 = new int[Integer.parseInt(dimension)];
							for (int i = 0; i < M0.length; i++) {
								String row = null;
								while ((row = in.nextLine()) != null
										&& (row.startsWith("#") || row
												.isEmpty()))
									;
								M0[i] = Integer.parseInt(row.trim());
							}
						}
						continue;
					}
					if (l.startsWith("Mf=")) {
						// Mf=Final Marking
						// Mf=#places
						String data[] = l.split("=");
						if (data.length == 2) {
							String dimension = data[1];
							Mf = new int[Integer.parseInt(dimension)];
							for (int i = 0; i < Mf.length; i++) {
								String row = null;
								while ((row = in.nextLine()) != null
										&& (row.startsWith("#") || row
												.isEmpty()))
									;
								Mf[i] = Integer.parseInt(row.trim());
							}
						}
						continue;
					}
				}
			} while (in.hasNextLine());
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		PetriNet nueva = PetriNet.createPetriNet(C, Pre, M0, Mf, d);
		nueva.V= V;
		return nueva;
	}

	public static void main(String[] args) {
		PetriTest testF = new PetriTest();
		PetriNet test = testF.readFromFile("/home/rogelio/Proyectos/Programacion Avanzada/mtiWorkspace/petri-nets/src/math/petrinets/model/input.pn");
		//Frame myFrame = new Frame("PetriNet"); // create frame with title
		/*
		 * Place p1 = new Place(8); p1.setName("place 1");
		 * 
		 * Place p2 = new Place(0); p2.setName("place 2");
		 * 
		 * Place p3 = new Place(0); p3.setName("place 3");
		 * 
		 * Place p4 = new Place(0); p4.setName("place 4");
		 * 
		 * test.add(p1); test.add(p2); test.add(p3); test.add(p4);
		 * 
		 * Transition t1 = new SimpleTransition(); t1.setName("t1") ;
		 * t1.addIn(p1, 2) ; t1.addOut(p2, 1) ;
		 * 
		 * Transition t2 = new SimpleTransition(); t2.setName("t2") ;
		 * t2.addIn(p2, 1) ; t2.addOut(p3, 1) ;
		 * 
		 * Transition t3 = new SimpleTransition(); t3.setName("t3");
		 * t3.addIn(p3,4); t3.addOut(p4,2);
		 * 
		 * test.add(t1); test.add(t2); test.add(t3);
		 * 
		 * 
		 * //test.add(t2) ; //test.add(t1) ;
		 * 
		 * test.init();
		 */
		test.drawNet();
		// add applet to the frame
		//myFrame.add(test, BorderLayout.CENTER);
		//myFrame.pack(); // set window to appropriate size (for its elements)
		//myFrame.setBounds(10, 10, 700, 600); // this time use a predefined frame
												// size/position
		//myFrame.setVisible(true); // usual step to make frame visible

		//test.updateField("one");
		/*
		while (true) {
			try {
				BufferedReader stdin = new BufferedReader(
						new InputStreamReader(System.in));
				test.updateField(stdin.readLine());
			} catch (Exception e) {
			}
		}*/
	}

}
